package com.example.springbean.pkg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Service
public class AsynService {

    private static final Logger logger = LoggerFactory.getLogger(AsynService.class);

    private List<String> movies =
            new ArrayList<>(
                    Arrays.asList(
                            "Forrest Gump",
                            "Titanic",
                            "Spirited Away",
                            "The Shawshank Redemption",
                            "Zootopia",
                            "Farewell ",
                            "Joker",
                            "Crawl"));

    @Async
    public CompletableFuture<List<String>> asynTask(String task) {
        logger.info(Thread.currentThread().getName() + "start this task!");


        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List<String> collect = movies.stream().filter(movie -> movie.startsWith(task)).collect(Collectors.toList());

        return CompletableFuture.completedFuture(collect);

    }


}
